package com.shop.aspect;

import com.shop.exception.LoginException;
import com.shop.util.SessionUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * description: 该文件说明
 *
 * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
 * @version 1.0
 * @date 2018/10/16 16:16
 **/
@Aspect
@Component
public class LoginAspect {

    @Pointcut("execution(public * com.shop.controller.BackStageController.*(..))" +
            "|| execution(public * com.shop.controller.BuyerManagerController.*(..))"+
            "|| execution(public * com.shop.controller.CategoryController.*(..))"+
            "|| execution(public * com.shop.controller.OrderDetailController.*(..))"+
            "|| execution(public * com.shop.controller.OrderMasterController.*(..))"+
            "|| execution(public * com.shop.controller.ProductController.*(..))")
    public void verify() {}
    @Around("verify()")
    public Object doVerify(ProceedingJoinPoint pjp) throws Throwable{
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Boolean loginFlag = SessionUtil.checkLogin(request);
        //如果没有登陆就拦截抛出异常
        if(!loginFlag){
            return "redirect:/admin/login";
        }
        return pjp.proceed();
    }
}
